// $codepro.audit.disable staticFieldNamingConvention, disallowUnnamedThreadUsage, useStartRatherThanRun, disallowDeprecatedMethodUsage, numericLiterals
/**
 * File Comment: Ita a GUI
 */
package edu.gatech.oad.project.presentation;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import javax.swing.JFrame;

import edu.gatech.oad.project.networking.clientSessions.loginSession;
import edu.gatech.oad.project.operations.users.User;

/**
 * This class is for testing purposes only.
 * @author James Moore and Devin 
 * @version 1.0
 */
public class DriverMain {
    
    /**
     * Field server: server connection
     */
    public static Socket server;
    
    /**
     * Field outBound: stream to the server
     */
    public static ObjectOutputStream outBound;
    
    /**
     * Field inBound: stream from the server
     */
    public static ObjectInputStream inBound;
    
    /**
     * Field token: security token
     */
    public static long token;
    
    /**
     * Field user: the active user
     */
    public static User user;
    
    /**
     * Field register: rather to register or login
     */
    public static Boolean register = false;
    
    /**
     * Field loginData: login data
     */
    public static String[] loginData = new String[] {"", "", ""};
    
    /**
     * Field FILEHANDLER: the file
     */
    public static FileHandler FILEHANDLER;
    
    /**
     * Field myLogger: holds the logget for the clients file
     */
    private static final Logger MYLOGGER = Logger.getLogger("edu.gatech." +
            "oad.project.presentation");
    
    static {
        FILEHANDLER = null;
        try {
            FILEHANDLER = new FileHandler("client.log");
        } catch (SecurityException e) {
            MYLOGGER.log(Level.SEVERE, "Security Exception " +
                    "creating the logger file handler", e);
            e.printStackTrace();
        } catch (IOException e) {
            MYLOGGER.log(Level.SEVERE, "IO Exception creating " +
                    "the logger file handler", e);
            e.printStackTrace();
        }
        
        FILEHANDLER.setFilter( new Filter() {
            public boolean isLoggable(LogRecord record) {
                //we would put whatever logic we need in here to 
                //determine if this should be logged.
                //for demo purposes, I am putting true
                return true;
            }
        });
        MYLOGGER.addHandler(FILEHANDLER);
        MYLOGGER.setLevel(Level.ALL);
        
    }
    
    /**
     * Starts the GUI thread
     * 
     * @param args command line arguements
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    final Driver driver = new Driver();
                    driver.getFrame().setPreferredSize(new Dimension(500, 500)); // $codepro.audit.disable numericLiterals
                    driver.getFrame().getContentPane().add(driver.getLogin());
					driver.getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    driver.getFrame().pack();
                    driver.getFrame().setVisible(true);
                } 
                catch (Exception e) {
                  e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Spawns a login session
     * 
     * @param username of the user
     * @param password of the user
     * @return the loginData
     */
    public static String[] loginToSystem(String username, String password) {
        
        final Thread thread = new Thread(new loginSession(username, password, "", true));
        thread.start();
        thread.run();
        thread.stop();
        if (!loginData[2].equals("")) { // $codepro.audit.disable numericLiterals
            return loginData;
        }
        return null; // $codepro.audit.disable returnValue
    }
    
    /**
     * Spawns a registration thread
     * 
     * @param username desired username
     * @param password desired password
     * @param userType the rank of the user
     * @return true if registered, false otherwise
     */
    public static boolean registerInSystem(String username,  // $codepro.audit.disable booleanMethodNamingConvention
    		String password, int userType){
        String rank;
        if (userType == 3) {
            rank = "admin";
        }
        else if (userType == 2) {
            rank = "doctor";
        }
        else if (userType == 1) {
            rank = "nurse";
        }
        else {
            rank = "patient";
        }
        final Thread thread = new Thread(new loginSession(
        		username, password, rank, false));
        thread.start();
        thread.run();
        thread.stop();
        System.out.println(DriverMain.register);
        return DriverMain.register;
    }

	/**
	 * @return a string
	 */
	@Override
	public String toString() {
		return "DriverMain [getClass()=" + getClass() + ", hashCode()="
				+ hashCode() + ", toString()=" + super.toString() + "]";
	}
    
}
